using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Collections;

namespace PSErrorLogToDB
{
    class Settings
    {
        private const string SETTINGS_NODE = "configuration/applicationSettings/PSErrorLogToDB.Properties.Settings";
        private static XmlDocument xmlDoc;
        private static string configfile;

        private static string dbserver;
        private static string dbname;
        private static string startdate;
        private static string enddate;
        private static ArrayList directories = new ArrayList();
        private static bool usefilter;

        static public string DBServer
        {
            get { return dbserver; }
            set { dbserver = value; }
        }

        static public string DBName
        {
            get { return dbname; }
            set { dbname = value; }
        }

        static public DateTime StartDate
        {
            get { return Convert.ToDateTime(startdate); }
            set { startdate = value.ToShortDateString(); }
        }

        static public DateTime EndDate
        {
            get { return Convert.ToDateTime(enddate); }
            set { enddate = value.ToShortDateString(); }
        }

        static public ArrayList Directories
        {
            get { return directories; }
            set { directories = value; }
        }

        static public bool UseFilter
        {
            get { return usefilter; }
            set { usefilter = value; }
        }

        static public void SaveSettings()
        {
            try
            {
                XmlNode nodeSettings = xmlDoc.SelectSingleNode(SETTINGS_NODE);
                if (nodeSettings != null)
                {
                    XmlNode nodeValue;
                    foreach (XmlNode node in nodeSettings.ChildNodes)
                    {
                        nodeValue = node.SelectSingleNode("value");
                        switch (node.Attributes["name"].Value)
                        {
                            case "DBServer":
                                nodeValue.InnerText = DBServer;
                                break;

                            case "DBName":
                                nodeValue.InnerText = DBName;
                                break;

                            case "StartDate":
                                nodeValue.InnerText = StartDate.ToShortDateString();
                                break;

                            case "EndDate":
                                nodeValue.InnerText = EndDate.ToShortDateString();
                                break;

                            case "UseFilter":
                                nodeValue.InnerText = UseFilter.ToString();
                                break;

                            case "Directories":
                                XmlNode nodeArray = nodeValue.SelectSingleNode("ArrayOfString");
                                if (nodeArray == null)
                                {
                                    nodeArray = xmlDoc.CreateNode(XmlNodeType.Element, "ArrayOfString", "");
                                }
                                nodeArray.RemoveAll();
                                XmlNode nodeItem;
                                foreach (string dir in Directories)
                                {
                                    nodeItem = xmlDoc.CreateNode(XmlNodeType.Element, "string", "");
                                    nodeItem.InnerText = dir;
                                    nodeArray.AppendChild(nodeItem);
                                }
                                break;
                        }
                    }
                }
                xmlDoc.Save(configfile);
            }
            catch (Exception ex)
            {
                throw new Exception("SaveSettings failed.  " + ex.Message);
            }
        }

        static public void LoadSettings(string configFile)
        {
            try
            {
                xmlDoc = new XmlDocument();
                configfile = configFile;
                xmlDoc.Load(configFile);

                XmlNode nodeSettings = xmlDoc.SelectSingleNode(SETTINGS_NODE);
                XmlNode nodeValue;
                foreach (XmlNode node in nodeSettings.ChildNodes)
                {
                    nodeValue = node.SelectSingleNode("value");
                    switch (node.Attributes["name"].Value)
                    {
                        case "DBServer":
                            DBServer = nodeValue.InnerText;
                            break;

                        case "DBName":
                            DBName = nodeValue.InnerText;
                            break;

                        case "StartDate":
                            if (nodeValue.InnerText.Length == 0)
                            {
                                StartDate = DateTime.Now.AddDays(-1);
                            }
                            else
                            {
                                StartDate = Convert.ToDateTime(nodeValue.InnerText);
                            }
                            break;

                        case "EndDate":
                            if (nodeValue.InnerText.Length == 0)
                            {
                                EndDate = DateTime.Now;
                            }
                            else
                            {
                                EndDate = Convert.ToDateTime(nodeValue.InnerText);
                            }
                            break;

                        case "UseFilter":
                            UseFilter = Convert.ToBoolean(nodeValue.InnerText);
                            break;

                        case "Directories":
                            Directories.Clear();
                            XmlNode nodeArray = nodeValue.SelectSingleNode("ArrayOfString");
                            if (nodeArray != null)
                            {
                                XmlNodeList nodeItems = nodeArray.SelectNodes("string");
                                foreach (XmlNode nodeItem in nodeItems)
                                {
                                    Directories.Add(nodeItem.InnerText);
                                }
                            }
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("LoadSettings failed.  " + ex.Message);
            }
        }
    }
}
